
<%
	if (session.getAttribute("username") == null) {
		%><jsp:forward page="/login.jsp" /><%
	}

	Object success = session.getAttribute("PMUserAssignSuccess");
	if(success != null && success.toString().equals("true")){
		success = "block";
		session.removeAttribute("PMUserAssignSuccess");
	}else{
		success = "none";
	}
%>
<%@ page language="java" contentType="text/html; charset=GBK"  
    pageEncoding="GBK" import="java.sql.*, connection.*"%>  

<%
String plistname = (String)session.getAttribute("plistname");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<link href="/DBP/style/stylesheet.css" rel="stylesheet" type="text/css" />  
<title>Assign Users to List</title>  
<script>

String.prototype.trim = function() {
	   return this.replace(/^\s+|\s+$/g,"");
}

function setList(){
	var checks = document.getElementsByTagName("input");
	var userlist = document.getElementById("userlist");
	var userselected = false;
	for (var i = 0; i < checks.length; ++i) {
		elem = checks[i]
		if(elem.type== "checkbox" && elem.id == "unassigned"){
			if(elem.checked){
				userselected = true;
				userlist.value += "," + elem.value.trim();
				//alert("."+elem.value.trim()+".");
			}
		}
	}
	document.getElementById("submit").type="submit";
	if(userselected == false){
		document.getElementById("usererror").style.display = "block";
		return false;
	}else{
		document.getElementById("usererror").style.display = "none";
	}
	return true;
}

function remove(){
	var checks = document.getElementsByTagName("input");
	var removelist = document.getElementById("removelist");
	var userselected = false;
	//alert("asdsa");
	for(var i = 0; i < checks.length; i++){
		elem = checks[i];
		      		//alert();
		if(elem.type== "checkbox" && elem.id == "remove"){
			//alert("."+elem.value.trim()+".");
			if(elem.checked){
				userselected = true;
				removelist.value += "," + elem.value.trim();
			}
		}
	}	
	if(userselect == false){
		document.getElementById("removeerror").style.display = "block";
		return false;
	}else{
		document.getElementById("removeerror").style.display = "none";
	}
	return true;
}
</script>
</head>
<body>
<div id="write"></div>
<div class="bg">
<div class="content">
<jsp:include page="../nav.jsp" />
<br/>
<div class="pagetitle">Poll Management > Assign users (<%=plistname %>)</div>
	<form action="AssignUserServlet" method="post"">
	<div class="entrytitle">
	Select users to assign:
	<div class="entry">
				<label for="username">Select users to assign:</label>
				<div class="tableborder" style="width:100%;"><table class="blockTable">
				<thead>
					<tr>
					<td style="width:1%;"></td><td>Username</td>
					</tr>
				</thead>
					<%
					DbConnection dbconn = new DbConnection();
					Connection conn = dbconn.getConn();
					String sql = "SELECT DISTINCT Username FROM PollUser WHERE accessibility = 1 AND Username NOT IN (SELECT DISTINCT Username FROM User_ParticipantList WHERE LName = '"+plistname+"')";
					System.out.println(sql);
					try{
						PreparedStatement ps = conn.prepareStatement(sql);
						ResultSet rs = ps.executeQuery();
						boolean usersFound = false;
						while(rs.next()){
							usersFound = true;
							String name = rs.getString("Username");
							out.write("<tr><td style=\"width:1%;\"><input type=\"checkbox\" id=\"unassigned\"name=\"user\" value=\" "+name+" \" /></td><td>"+name+"</td></tr>");
						}
						if(!usersFound){
							out.write("<tr><td>No users are available to assign</td></tr>");
						}
					}catch(SQLException e){
						System.out.println(e.toString());
						e.printStackTrace();
					}
					%>
				</table>
				<input style="width:100px; white-space:normal;" type="submit" onClick="return setList()" class="button" id="submit" value="Assign to <%=plistname%>"/>
				</div>
				<input type="hidden"  id="selectlist"  name="partlist" value="<%=plistname%>">
				<input type="hidden" name="userlist" value="init" id="userlist" />
				<div class="error" id="usererror">Please select a user!</div>
			
			</div>
			</div>
			<div style="margin-left: 40%;margin-bottom:10px">
			<img style="width:100px; height:100px;" src="/DBP/images/arrow_down.gif"/>
			</div>
		</form>
			<div class="entrytitle">
			Users in <%=plistname %>
			<div class="entry">
			<form action="AssignUserServlet?remove=true" method="post"">
					<div class="tableborder" style="width:100%;"><table class="blockTable">
					<thead>
						<tr>
						<td width="5px;"></td><td>Username</td>
						</tr>
					</thead>
						<%
						String sql2 = "SELECT DISTINCT Username FROM User_ParticipantList WHERE LName = '"+plistname+"'" ;
						System.out.println(sql2);
						try{
							ResultSet rs2 = conn.prepareStatement(sql2).executeQuery();
							while(rs2.next()){
								String name2 = rs2.getString("Username");
								out.write("<tr><td width=\"5px;\"><input id=\"remove\"  value=\""+name2+"\" type=\"checkbox\" /></td><td>"+name2+"</td></tr>");
							}
						}catch(SQLException e){
							System.out.println(e.toString());
							e.printStackTrace();
						}
						dbconn.close();
						%>
					</table></div>
					<input type="hidden"  id="selectlist"  name="partlist" value="<%=plistname%>">
					<input type="hidden" name="removelist" value="init" id="removelist" />
					<input style="width:100px; white-space:normal;" onClick="return remove()" class="button" type="submit" id="submitremove" value="Remove from <%=plistname%>"/>
					<div class="error" id="removeerror">Please select a user!</div>
			</form>
			<div class="note" id="success" style="display:<%=success %>">Success! Users have been assigned.</div>
			</div>
			</div>
		</div>
</div>
</body>  
</html>  
